package xiangmu

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhuandan/data_source"
	"zhuandan/models"
)

func Wendangtypes(ctx *gin.Context) {
	var users []models.Wendangtype
	data_source.Db.Order("id desc").Find(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

type WendangtypeBody struct {
	ID   int64  `json:"id" gorm:"id"`
	Name string `json:"name" gorm:"name"`
}

func Wendangtypeadd(ctx *gin.Context) {
	request := WendangtypeBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	huiyiadd := models.Wendangtype{
		Name: name,
	}
	data_source.Db.Create(&huiyiadd)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Wendangtypeupdate(ctx *gin.Context) {
	request := WendangtypeBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	huiyiadd := models.Wendangtype{
		Name: name,
	}
	data_source.Db.Where("id = ?", id).Updates(&huiyiadd)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Wendangtypedel(ctx *gin.Context) {
	request := WendangtypeBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Wendangtype{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type WendangsRBody struct {
	Data       []models.Wendang `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}

type WendangsBody struct {
	Page          int   `json:"pageNo" from:"pageNo"`
	Pagesize      int   `json:"pageSize" from:"pageSize"`
	Wendangtypeid int64 `json:"wendangtypeid" gorm:"wendangtypeid"`
}

func Wendangs(ctx *gin.Context) {
	request := WendangsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	wendangtypeid := request.Wendangtypeid
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Wendang
	res := data_source.Db.Where("wendangtypeid = ?", wendangtypeid)
	res.Model(&models.Wendang{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := WendangsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type WendangBody struct {
	ID            int64  `json:"id" gorm:"id"`
	Name          string `json:"name" gorm:"name"`
	Addtime       int64  `json:"addtime" gorm:"addtime"`
	URL           string `json:"url" gorm:"url"`
	Wendangtypeid int64  `json:"wendangtypeid" gorm:"wendangtypeid"`
}

func Wendangadd(ctx *gin.Context) {
	request := WendangBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	wendangtypeid := request.Wendangtypeid
	url := request.URL
	wendang := models.Wendang{
		Name:          name,
		Wendangtypeid: wendangtypeid,
		URL:           url,
	}
	data_source.Db.Create(&wendang)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Wendangupdate(ctx *gin.Context) {
	request := WendangBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	wendangtypeid := request.Wendangtypeid
	url := request.URL
	wendang := models.Wendang{
		Name:          name,
		Wendangtypeid: wendangtypeid,
		URL:           url,
	}
	data_source.Db.Where("id = ?", id).Updates(&wendang)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Wendangdel(ctx *gin.Context) {
	request := WendangBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Wendang{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
